<template>
	<view class="next-list">
		<scroll-view @scroll="scrollCallback" class="next-scroll-left" scroll-y="true" :scroll-with-animation="false" :scroll-into-view="scrollIntoView">
			<view class="next-search" id="TOP">
				<image class="next-search-img" :src="nextSearchImgSrc"></image>
				<input class="next-input" @input="search" v-model="searchStr" placeholder="搜索" maxlength="50" placeholder-class="next-placeholder" />
			</view>
			<view class="left-list" v-for="(item, index) of scrollLeftObj" :key="index" :id="index != '#' ? index : 'BOTTOM'">
				<view :id="`item_${index}`">
					<view
						ref="itemType"
						:style="{ borderBottom: index == scrollIntoViewCopy && isborder ? '1px solid #eeeeee' : '' }"
						class="left-item-title"
						v-if="item && item.length"
					>
						{{ index }}
					</view>
					<view class="left-item-card" v-for="(mess, inx) in item" :key="inx" @click.stop="chooseItem(mess)">
						<view v-if="showAvatar">
							<image
								:style="'border-radius:' + radius"
								class="left-item-card-img img-info"
								:src="mess[imgKey]"
								v-if="mess[imgKey]"
								@click.stop="preview(mess[imgKey])"
							></image>
							<view :style="'border-radius:' + radius" class="left-item-card-img" v-else>
								{{ (mess[nameKey] && mess[nameKey].slice(0, 1)) || '' }}
							</view>
						</view>
						<view class="left-item-card-info" :style="inx < item.length - 1 ? 'border-bottom: solid #F4F4F4 1rpx;' : ''">
							<slot v-if="$slots.name" name="name" :data="mess"></slot>
							<view v-else class="left-item-card-name">{{ mess[nameKey] || '' }}</view>
							<!-- 	<slot v-if="$slots.phone" name="phone" :data="mess"></slot>
							<view class="left-item-card-phone" v-else-if="mess[phoneKey]">{{mess[phoneKey]}}</view> -->
						</view>
					</view>
				</view>
			</view>
			<view v-if="hasData" class="texts" style="height: 100rpx">{{ friends }}个朋友</view>
			<view class="no-data" v-if="!hasData">
				<image class="no-data-img" :src="noDataImgSrc"></image>
				<view class="no-data-name">查无此人</view>
			</view>
		</scroll-view>
		<view class="next-scroll-right" v-if="hasData">
			<image class="next-scroll-right-top" :src="nextScrollRightTopSrc" @click.stop="scrollTop"></image>
			<view
				:class="{ 'next-scroll-right-name': true, 'next-scroll-right-select': item == scrollIntoViewCopy }"
				v-for="(item, index) in scrollRightList"
				:key="index"
				@click.stop="chooseType(item)"
			>
				{{ item }}
				<view v-if="isshow && item == scrollIntoViewCopy" class="uni-indexed-list__alert-wrapper">
					<view class="uni-indexed-list__alert">
						<text class="">{{ scrollIntoViewCopy }}</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import nextSearchImgSrc from '../../static/search.png';
import noDataImgSrc from '../../static/noData.png';
import nextScrollRightTopSrc from '../../static/top.png';
import pinyin from './js-pinyin/dist/pinyin.js';
// 创建pinyin实例
const pinyinInstance = new pinyin({
	charCase: 0
});

const position = {};

export default {
	props: {
		dataList: {
			type: Array,
			required: true,
			default: () => {
				return [];
			}
		},
		//显示的主键key值
		idKey: {
			type: String,
			default: 'id'
		},
		nameKey: {
			type: String,
			default: 'name'
		},
		phoneKey: {
			type: String,
			default: 'phone'
		},
		imgKey: {
			type: String,
			default: 'img'
		},
		radius: {
			type: String,
			default: '6rpx'
		},
		showAvatar: {
			type: Boolean,
			default: true
		},
		isInterlock: {
			type: Boolean,
			default: true
		}
	},
	data() {
		return {
			searchStr: '',
			scrollIntoView: '',
			scrollIntoViewCopy: '',
			scrollLeftObj: {},
			oldObj: {},
			scrollRightList: [],
			hasData: true,
			nextSearchImgSrc,
			noDataImgSrc,
			nextScrollRightTopSrc,
			friends: 0,
			isshow: false,
			isborder: false,
			topHeight: 0,
			windowHeight: ''
		};
	},
	created() {
		let that = this;
		if (that.dataList.length != 0) {
			that.$watch(
				() => that.dataList,
				(newList) => {
					that.friends = that.dataList.length;
					if (newList && newList.length) that.cleanData(newList);
					if (that.isInterlock) {
						that.$nextTick(() => {
							uni.getSystemInfo({
								success: function (res) {
									that.windowHeight = res.windowHeight + 'px';
								}
							});
							Object.keys(that.scrollRightList).map((key) => {
								const query = uni.createSelectorQuery().in(that).select(`#item_${that.scrollRightList[key]}`);
								query
									.boundingClientRect((res) => {
										const { bottom } = res;
										position[that.scrollRightList[key]] = bottom;
									})
									.exec();
							});
						});
					}
				},
				{
					immediate: true,
					deep: true
				}
			);
		}
	},
	methods: {
		getlength(object) {
			let data = [object];
			data.forEach((item, index) => {
				for (const key in item) {
					if (item[key].length) {
						this.friends = item[key].length;
					}
				}
			});
		},
		search() {
			if (this.searchStr) {
				let has = false;
				this.scrollLeftObj = JSON.parse(JSON.stringify(this.oldObj));
				for (let i in this.scrollLeftObj) {
					this.scrollLeftObj[i] = this.scrollLeftObj[i].filter((item) => {
						return item[this.nameKey].indexOf(this.searchStr) != -1 || item[this.phoneKey].indexOf(this.searchStr) != -1;
					});
					if (this.scrollLeftObj[i].length) has = true;
				}
				if (has) this.hasData = true;
				else this.hasData = false;
				this.getlength(this.scrollLeftObj);
			} else {
				this.hasData = true;
				this.scrollLeftObj = JSON.parse(JSON.stringify(this.oldObj));
				this.friends = this.dataList.length;
			}
		},
		scrollCallback(e) {
			const getOneHeight = uni.createSelectorQuery().in(this).select(`#item_${this.scrollRightList[0]}`);
			getOneHeight
				.boundingClientRect((res) => {
					this.topHeight = res.top;
				})
				.exec();
			const { detail } = e;
			const { scrollTop, scrollHeight } = detail;
			if (scrollTop >= this.topHeight) {
				this.isborder = true;
			} else {
				this.isborder = false;
			}
			if (this.scrollIntoView === 'TOP') {
				this.scrollIntoView = '';
			}
			if (this.isInterlock) {
				for (let key in position) {
					if (position[key] > scrollTop + 10 && position[key] - scrollTop <= scrollHeight) {
						this.scrollIntoViewCopy = key;
						return;
					}
				}
			}
		},
		scrollTop() {
			this.scrollIntoView = 'TOP';
		},
		cleanData(list) {
			this.scrollRightList = this.getLetter();
			let newList = [];
			list.forEach((res) => {
				let initial = pinyinInstance.getCamelChars(res.name.trim());
				let firsfirs = initial ? initial.substring(0, 1) : '';
				if (!newList[firsfirs]) newList[firsfirs] = [];
				newList[firsfirs].push({
					[this.idKey]: res[this.idKey] || '',
					[this.nameKey]: res[this.nameKey].trim() || '',
					[this.phoneKey]: res[this.phoneKey] || '',
					[this.imgKey]: res[this.imgKey] || ''
				});
			});
			this.scrollRightList.forEach((t) => {
				if (newList[t]) {
					this.$set(this.scrollLeftObj, t, newList[t]);
				} else {
					this.$set(this.scrollLeftObj, t, []);
				}
			});
			let surplusList = [];
			for (var i in newList) {
				let han = this.scrollRightList.find((v) => {
					return v == i;
				});
				if (!han) surplusList.push(newList[i]);
			}
			surplusList.forEach((item) => {
				this.$set(this.scrollLeftObj, '#', item);
			});
			this.oldObj = JSON.parse(JSON.stringify(this.scrollLeftObj));

			// 过滤不存在的关键索引
			const existList = Object.keys(this.scrollLeftObj).filter((key) => {
				return this.scrollLeftObj[key].length;
			});
			this.scrollRightList = this.scrollRightList.filter((key) => {
				return existList.some((k) => k === key);
			});
		},
		getLetter() {
			let list = [];
			for (var i = 0; i < 26; i++) {
				list.push(String.fromCharCode(65 + i));
			}
			list.push('#');
			return list;
		},
		chooseType(item) {
			console.log(item, this.scrollIntoViewCopy, '------------');
			if (item == '#') {
				item = 'BOTTOM';
			}
			setTimeout(() => {
				this.isshow = true;
				this.scrollIntoView = item;
				this.scrollIntoViewCopy = item;
				setTimeout(() => {
					this.isshow = false;
				}, 500);
			}, 50);
		},
		preview(img) {
			uni.previewImage({
				current: 0,
				urls: [img]
			});
		},
		chooseItem(item) {
			this.$emit('itemclick', item);
		}
	}
};
</script>
<style>
/deep/ ::-webkit-scrollbar {
	width: 0;
	height: 0;
	color: transparent;
	display: none;
}
</style>
<style lang="scss" scoped>
.texts {
	color: #777777;
	text-align: center;
	line-height: 100rpx;
	font-size: 26rpx;
}

.next-list {
	width: 100%;
	box-sizing: border-box;
	.next-search {
		width: 100%;
		height: 106rpx;
		background-color: #ffffff;
		display: flex;
		align-items: center;
		justify-content: center;
		.next-search-img {
			width: 32rpx;
			height: 32rpx;
			position: absolute;
			left: 24rpx;
		}
		.next-input {
			width: calc(100% - 24rpx);
			height: 72rpx;
			background: #eeeeee;
			border-radius: 10rpx;
			padding: 0 32rpx 0 50rpx;
			box-sizing: border-box;
			color: #333333;
			font-size: 30rpx;
		}
		.next-placeholder {
			color: #666666;
		}
	}

	.next-scroll-left {
		height: 100vh;
		.left-list {
			height: auto;
			.left-item-title {
				width: calc(100% - 24rpx);
				height: 60rpx;
				padding-left: 24rpx;
				text-align: left;
				line-height: 60rpx;
				font-size: 30rpx;
				color: #666666;
				position: sticky;
				top: 0%;
				background-color: #ffffff;
				z-index: 9;
			}

			.left-item-card {
				width: 100%;
				height: 112rpx;
				background-color: #ffffff;
				box-sizing: border-box;
				padding-left: 24rpx;
				display: flex;
				align-items: center;
				justify-content: flex-start;

				.left-item-card-img {
					width: 80rpx;
					min-width: 80rpx;
					height: 80rpx;
					background-color: #cfcfcf;
					display: flex;
					align-items: center;
					justify-content: center;
					font-size: 36rpx;
					font-weight: bold;
					color: #ffffff;
				}

				.img-info {
					background: none;
					border: solid #f0f0f0 1rpx;
				}

				.left-item-card-info {
					width: 100%;
					margin-left: 32rpx;
					height: 100%;
					display: flex;
					align-items: flex-start;
					justify-content: center;
					flex-direction: column;

					.left-item-card-name {
						font-size: 30rpx;
						line-height: 30rpx;
						color: #333333;
					}

					.left-item-card-phone {
						margin-top: 14rpx;
						font-size: 28rpx;
						line-height: 28rpx;
						color: #999999;
					}
				}
			}
		}

		.no-data {
			width: 100%;
			display: flex;
			align-items: center;
			justify-items: center;
			flex-direction: column;
			margin-top: 25%;

			.no-data-img {
				width: 200rpx;
				height: 200rpx;
			}

			.no-data-name {
				margin-top: 20rpx;
				font-size: 28rpx;
				color: #666666;
			}
		}
	}

	.next-scroll-right {
		position: fixed;
		right: 0rpx;
		top: 50%;
		transform: translateY(-47%);
		z-index: 999 !important;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;

		.next-scroll-right-top {
			width: 32rpx;
			height: 32rpx;
			margin-right: 10rpx;
			z-index: 999 !important;
		}

		.next-scroll-right-name {
			width: 32rpx;
			padding-right: 10rpx;
			height: 28rpx;
			font-size: 22rpx;
			color: #333333;
			line-height: 22rpx;
			margin-top: 8rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			position: relative;
		}

		.next-scroll-right-select {
			padding: 0;
			margin-right: 14rpx;
			width: 28rpx;
			height: 28rpx;
			border-radius: 50%;
			background: #d81e06;
			color: #ffffff;
		}
	}
}

.uni-indexed-list__alert-wrapper {
	position: absolute;
	width: 55px;
	height: 55px;
	transform: rotate(-45deg);
	right: 60rpx;
}

.uni-indexed-list__alert {
	width: 50px;
	height: 50px;
	border-radius: 100px 100px 0 100px;
	text-align: center;
	background-color: #c9c9c9;
	display: flex;
	justify-content: center;
	align-items: center;
	color: #ffffff;

	text {
		font-size: 24px;
		line-height: 24px;
		font-weight: bold;
		color: #fff;
		transform: rotate(45deg);
		text-align: center;
	}
}
</style>
